package com.homedo.odin.solution.service;

import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.StringUtil;
import com.homedo.odin.solution.contract.mng.HotKeywordEditReq;
import com.homedo.odin.solution.contract.oms.HotKeywordListReq;
import com.homedo.odin.solution.contract.oms.HotKeywordListResp;
import com.homedo.odin.solution.contract.pc.PcHotKeywordListResp;
import com.homedo.odin.solution.dao.HotKeywordMapper;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.model.entity.HotKeyword;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p> 热搜关键词  服务实现类 </p>
 *
 * @author ZhangLei
 * @date 2018-05-15
 * @Description:
 */
@Service
public class HotKeywordServiceImpl extends BaseService<HotKeywordMapper, HotKeyword> {

    /**
     * 获取所有的热搜关键词
     */
    public HotKeywordListResp queryHotKeywords() {
        HotKeywordListResp resp = new HotKeywordListResp();
        HotKeyword queryHotKeyword = new HotKeyword();
        queryHotKeyword.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
        List<HotKeyword> hotKeywords = baseMapper.queryHotKeywords(queryHotKeyword);
        resp.setList(hotKeywords);
        //获取最近修改时间和修改人
        HotKeyword lateHotword = baseMapper.queryLateHotKeyword();
        resp.setModifiedBy(lateHotword.getModifiedBy());
        resp.setModifiedTime(lateHotword.getModifiedTime());
        resp.setOperator(lateHotword.getOperator());
        return resp;
    }

    /**
     * PC端获取所有的热搜关键词
     */
    public PcHotKeywordListResp queryPcHotKeywords(Integer top) {
        PcHotKeywordListResp resp = new PcHotKeywordListResp();
        HotKeyword queryHotKeyword = new HotKeyword();
        queryHotKeyword.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
        List<HotKeyword> hotKeywords = baseMapper.selectAll(top);
        resp.setList(hotKeywords);
        return resp;
    }

    /**
     * @Description: OMS_热搜关键词修改
     * @Author: liyi
     */
    public void updateHotKeywords(HotKeywordListReq req) {
        /*
         * 操作步骤
         * 第一步把全部记录都变成不可用，
         * 第二步
         * 当关键词没变时，把原来相同关键词的变成可用，并更新对应状态。
         * 当关键词变了，添加一条新数据
         */
        baseMapper.updateHotkeywordStatus(DeleteEnum.IS_DEL.getDbValue());

        List<HotKeywordEditReq> hotKeywordEditList = req.getList();
        hotKeywordEditList.forEach(hotKeywordEditReq -> {
            String name = hotKeywordEditReq.getName();
            HotKeyword queryHotKeyword = new HotKeyword();
            queryHotKeyword.setName(name);
            HotKeyword hotKeyword = baseMapper.queryHotKeyword(queryHotKeyword);
            //当关键词没变时，
            if (hotKeyword != null) {
                hotKeyword.setModifiedTime(new Date());
                hotKeyword.setModifiedBy(hotKeywordEditReq.getModifiedBy());
                hotKeyword.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
                hotKeyword.setHighlight(hotKeywordEditReq.getHighlight());
                hotKeyword.setSort(hotKeywordEditReq.getSort());
                hotKeyword.setOperator(hotKeywordEditReq.getOperator());
                updateById(hotKeyword);
            } else {
                //当关键词变了，添加一条新数据
                if (!StringUtil.isEmpty(name)) {
                    HotKeyword hotKeywordNew = BeanUtils.transfrom(HotKeyword.class, hotKeywordEditReq);
                    hotKeywordNew.setCreatedTime(new Date());
                    hotKeywordNew.setModifiedTime(new Date());
                    hotKeywordNew.setCreatedBy(hotKeywordEditReq.getModifiedBy());
                    hotKeywordNew.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
                    insert(hotKeywordNew);
                }
            }
        });
    }
}
